﻿
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer.Stream.Extension;
using System.Net.DnsToolkit.Packet.Busy;

namespace System.Net.DnsToolkit.Bastard.PacketGenerator {
    /// <summary>
    /// Create Huge Question Packets
    /// </summary>
    public class HugeQuestionPacket : IPacketGenerator {

        const string defaultFormatName = "h{0}.localhost.localnet";
        const ushort defaultQCount = 49;

        public HugeQuestionPacket()
            : this(defaultQCount, defaultFormatName) {
        }
        public HugeQuestionPacket(ushort qcount)
            : this(qcount, defaultFormatName) {
        }

        public HugeQuestionPacket(ushort qcount, string formatName) {
            _qcount = qcount;
            _name = formatName;
        }

        private readonly ushort _qcount;
        private readonly string _name;

        /// <summary>
        /// Huge Packet full of questions
        /// </summary>
        /// <returns></returns>
        public byte[] GetBytes() {
            return GetMessage().GetBytes();

        }
        // <summary>
        /// Huge Packet full of questions
        /// </summary>
        /// <returns></returns>
        public IMessage GetMessage() {

            var message = new Message();

            for (int i = 0; i < _qcount; i++) {
                var q = GetQuestion(string.Format(_name, i));
                message.Questions.Add(q);
            }
            message.Header.QuestionCount = (ushort)message.Questions.Count;

            return message;

        }

        private static Question GetQuestion(string name) {

            var question = new Question {
                QClass = QClassEnum.IN,
                QType = QTypeEnum.A,
                QName = new DomainName {
                    Name = name
                }
            };
            return question;
        }
    }
}
